package com.selfdriving;

import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.selfdriving.bean.Home;

public class BaseApplication extends Application
{
	public static final String CURRENT_CITY_ID = "101240203";

	public final static String strKey = "5f7c77f3813f89b30e65e44d34514c4f";
	public LocationClient mLocClient;
	public BMapManager mBMapManager = new BMapManager(this);
	public double mLongitude = 0;
	public double mLatitude = 0;
	boolean isflag = true;
	public String mUploadPhotoPath;// 相册路径
	public GeoPoint geoPoint = null;
	public List<Home> homeList = new ArrayList<Home>();

	@Override
	public void onCreate()
	{
		super.onCreate();
		initEngineManager(this);

		// 获取当前用户位置
		mLocClient = new LocationClient(getApplicationContext());
		mLocClient.setAK(strKey);
		mLocClient.registerLocationListener(new BDLocationListener()
		{

			@Override
			public void onReceivePoi(BDLocation location)
			{

			}

			@Override
			public void onReceiveLocation(BDLocation location)
			{
				mLongitude = location.getLongitude();
				mLatitude = location.getLatitude();
				System.out.println("地理位置" + "经度:" + mLongitude + ",纬度:"
						+ mLatitude);
				mLocClient.stop();
			}
		});
		mLocClient.start();
		mLocClient.requestOfflineLocation();
		System.out.println("开始获取");
	}

	@Override
	public void onLowMemory()
	{
		super.onLowMemory();
		Log.e("BaseApplication", "onLowMemory");
	}

	@Override
	public void onTerminate()
	{
		super.onTerminate();
		Log.e("BaseApplication", "onTerminate");
	}

	public void initEngineManager(Context context)
	{
		if (mBMapManager == null)
		{
			isflag = true;
			mBMapManager = new BMapManager(context);
		}

		if (!mBMapManager.init(strKey, new MyGeneralListener(
				BaseApplication.this.getApplicationContext())))
		{
			isflag = false;
			Toast.makeText(BaseApplication.this.getApplicationContext(),
					"BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
		}
		mBMapManager.start();
	}

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener
	{
		Context context;

		public MyGeneralListener(Context context)
		{
			this.context = context;
		}

		@Override
		public void onGetNetworkState(int iError)
		{
			if (iError == MKEvent.ERROR_NETWORK_CONNECT)
			{
				showLongToast(context, "您的网络出错啦！");
			} else if (iError == MKEvent.ERROR_NETWORK_DATA)
			{
				showLongToast(context, "输入正确的检索条件！");
			}
		}

		@Override
		public void onGetPermissionState(int iError)
		{
			if (iError == MKEvent.ERROR_PERMISSION_DENIED)
			{
				// 授权Key错误：

				showLongToast(context, "请在 ApplicationConfig.java文件输入正确的授权Key！");
			}
		}

	}

	/** 长时间显示Toast提示(来自String) **/
	public static void showLongToast(Context context, String text)
	{
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}
}
